<?php

class Artist {

    private $_id;
    private $_name;
    private $_anv;
    private $_join;
    private $_role;
    private $_tracks;

    public function getId() {
        return $this->_id;
    }

    public function setId($id) {
        $this->_id = $id;
        return $this;
    }

    public function getName() {
        return $this->_name;
    }

    public function setName($name) {
        $this->_name = $name;
        return $this;
    }

    public function getAnv() {
        return $this->_anv;
    }

    public function setAnv($anv) {
        $this->_anv = $anv;
        return $this;
    }

    public function getJoin() {
        return $this->_join;
    }

    public function setJoin($join) {
        $this->_join = $join;
        return $this;
    }

    public function getRole() {
        return $this->_role;
    }

    public function setRole($role) {
        $this->_role = $role;
        return $this;
    }

    public function getTracks() {
        return $this->_tracks;
    }

    public function setTracks($tracks) {
        $this->_tracks = $tracks;
        return $this;
    }

    function __construct(array $donnees) {
        foreach ($donnees as $key => $value) {
            if (isset($donnees[$key][0]['__content__'])) {
                $donnees[$key] = $donnees[$key][0]['__content__'];
            } else {
                $donnees[$key] = $donnees[$key][0];
            }
        }

        $this->hydrate($donnees);
    }

    public function __toString() {
        return print_r($this->__toArray(), true);
    }

    public function __toArray() {
        return array('anv' => $this->_anv,
            'id' => $this->_id,
            'join' => $this->_join,
            'name' => $this->_name,
            'role' => $this->_role,
            'tracks' => $this->_tracks);
    }

    public function hydrate(array $donnees) {
        foreach ($donnees as $key => $value) {
// On récupère le nom du setter correspondant à l'attribut.
            $method = 'set' . ucfirst($key);

// Si le setter correspondant existe.
            if (method_exists($this, $method)) {
// On appelle le setter.
                $this->$method($value);
            }
        }
    }

}

?>
